package com.company.genealogy.web.service.impl;

import com.company.genealogy.web.entity.Region;
import com.company.genealogy.web.mapper.RegionMapper;
import com.company.genealogy.web.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {
    
    @Autowired
    private RegionMapper regionMapper;
    
    @Override
    public List<Region> getRegionsByParentCode(String parentCode) {
        return regionMapper.selectByParentCode(parentCode);
    }
    
    @Override
    public List<Region> getRegionsByLevel(Integer level) {
        return regionMapper.selectByLevel(level);
    }
    
    @Override
    public Region getRegionByCode(String code) {
        return regionMapper.selectByCode(code);
    }
    
    @Override
    public List<Region> getProvinces() {
        return getRegionsByLevel(1);
    }
    
    @Override
    public List<Region> getCities(String provinceCode) {
        return getRegionsByParentCode(provinceCode);
    }
    
    @Override
    public List<Region> getCounties(String cityCode) {
        return getRegionsByParentCode(cityCode);
    }
    
    @Override
    public List<Region> getTowns(String countyCode) {
        return getRegionsByParentCode(countyCode);
    }
    
    @Override
    public List<Region> getRegionTree() {
        // 获取所有省份
        List<Region> provinces = getProvinces();
        
        // 为每个省份添加城市信息
        for (Region province : provinces) {
            List<Region> cities = getCities(province.getCode());
            for (Region city : cities) {
                List<Region> counties = getCounties(city.getCode());
                city.setChildren(counties);
            }
            province.setChildren(cities);
        }
        
        return provinces;
    }
} 